package com.haixiaoke.saas.repository.service.impl;

import com.haixiaoke.saas.repository.constant.CacheConstant;
import com.haixiaoke.saas.repository.mapper.AreaMapper;
import com.haixiaoke.saas.repository.pojo.Area;
import com.haixiaoke.saas.repository.service.IAreaService;
import com.haixiaoke.saas.repository.util.Tree.Node;
import com.haixiaoke.saas.repository.util.Tree.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 区域选择Service
 */
@Service
public class AreaServiceImpl implements IAreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    @Cacheable(CacheConstant.AREA_LEVEL2_OPTIONS)
    public List<Node> getLevel2() {
        List<Node> level2 = areaMapper.getLevel2();
        Tree tree = new Tree(level2);
        return tree.buildTree();
    }

    @Override
    @Cacheable(CacheConstant.AREA_LEVEL3_OPTIONS)
    public List<Node> getLevel3() {
        List<Node> level3 = areaMapper.getLevel3();
        Tree tree = new Tree(level3);
        return tree.buildTree();
    }

    @Override
    public List<Area> selectAreaList(Long[] areaId) {
        return areaMapper.selectAreaList(areaId);
    }

    /**
     * 根据城市 id 查询区
     * @param cityId
     * @return
     */
    @Override
    public List<Node> areaComboList(String cityId) {
        return areaMapper.selectAreaComboList(cityId);
    }

    @Override
    public Long selectCityByDistrictId(Long districtId) {
        return areaMapper.selectCityByDistrictId(districtId);
    }

}
